#!/usr/bin/env python

"""

    Main file for pyews application
    call start stop restart method from here

"""

from irc import irc
import sys, time, os.path, ConfigParser
from daemon import Daemon

class PyEwsDaemon(Daemon):
    PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
    configfile = os.path.join(PROJECT_PATH, "pyews.conf")
    config = ConfigParser.ConfigParser()
    config.read(configfile)
    server = config.get("irc", "server")
    port = config.getint("irc", "port")
    nick = config.get("irc", "nick")
    user = config.get("irc", "user")
    channel = config.get("irc", "channel")
    source = config.get("irc", "source")

    def run(self):
        ews = irc(self.server, self.port, self.nick, self.user, self.channel, self.source)
        ews.start()

if __name__ == "__main__":
	daemon = PyEwsDaemon('/var/run/pyewsdaemon.pid')
	if len(sys.argv) == 2:
		if 'start' == sys.argv[1]:
			daemon.start()
		elif 'stop' == sys.argv[1]:
			daemon.stop()
		elif 'restart' == sys.argv[1]:
			daemon.restart()
		else:
			print "Unknown command"
			sys.exit(2)
		sys.exit(0)
	else:
		print "usage: %s start|stop|restart" % sys.argv[0]
		sys.exit(2)
